<?php

try {

    class gitCmd {

        public function tag($args) {

            if ($args) {
                echo "path->" . '.git/hooks/cf/' . $args . "\r\n";
                if (!file_exists('.git/hooks/cf/' . $args)) {
                    $result = mkdir('.git/hooks/cf/' . $args, 777, true);
                }
                if (file_exists('.git/hooks/cf/' . $args)) {
                    $changeFiles = shell_exec("git diff --cached --name-only");
                    file_put_contents('.git/hooks/cf/' . $args . '/logs.log', $changeFiles, FILE_APPEND);
                    $files = explode("\n", $changeFiles);
                    if ($files) {
                        $itemOutput = '';
                        $itemHashOutput = '';
                        $hash = '';
                        if (file_exists('.git/hooks/cf/' . $args . '/hash.log')) {
                            $hash = file_get_contents('.git/hooks/cf/' . $args . '/hash.log');
                        }
                        foreach ($files as $item) {
                            if (!empty($item) && (empty($hash) || stripos($hash, md5($item)) === FALSE)) {
                                $itemOutput.=("\n" . $item);
                                $itemHashOutput .=(md5($item) . ',');
                            }
                        }
                        if ($itemOutput) {
                            file_put_contents('.git/hooks/cf/' . $args . '/files.log', $itemOutput, FILE_APPEND);
                            file_put_contents('.git/hooks/cf/' . $args . '/hash.log', $itemHashOutput, FILE_APPEND);
                        }
                    }
                }
            }
        }

        public function tip($args)
        {
        	if($args)
        	{
        		echo "weekly!";
        		$file = date('Y_m_W')."_logs.log";
        		file_put_contents('E:/weekly/'.$file, $args, FILE_APPEND);
        	}
        }

    }

    if (isset($argv[1])) {
        $message = file_get_contents($argv[1]);
        if ($message) {
            preg_match_all('/\s-{1}(\w+\s+)(\w+)/', $message, $matches);
            $cmd = [];
            if (isset($matches[1]) && isset($matches[2])) {
                foreach ($matches[1] as $key => $item) {
                    $cmd[trim($item)] = $matches[2][$key];
                }
            }
            $gitCmd = new gitCmd();
            foreach ($cmd as $method => $params) {
                if (method_exists($gitCmd, $method)) {
                    $resutl = call_user_func_array(array($gitCmd, $method), [$params]);
                }
            }
            //$a = shell_exec("git config --get user.name");
        }
    }
} catch (Exception $ex) {
    echo $ex->getMessage();
}

